package com.ihr360.cnb.module.payroll.controller;

import cn.irenshi.entity.account.WebUserSimpleEntity;
import com.google.common.collect.Lists;
import com.ihr360.authority.sharing.model.vo.SimpleAuthUserResourceVo;
import com.ihr360.cnb.module.oneclick.service.IrenshiDubboProviderService;
import com.ihr360.cnb.module.payroll.service.PayrollService;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.type.AccessPrivilege;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-07 19:43:40.
 */
@RestController
@RequestMapping("/payroll/authority")
public class AuthorityAggregateController {

    @Autowired
    private IrenshiDubboProviderService irenshiDubboProviderService;

    @Autowired
    private PayrollService payrollService;

    @ApiOperation("获取资源可操作的全部用户列表")
    @PostMapping(value = "/user/list")
    public List<SimpleAuthUserResourceVo> getUsers(@RequestParam(value = "companyId") String companyId,
                                                   @RequestParam(value = "bizModel") String bizModelAlias,
                                                   @RequestParam(value = "resourceId") String resourceId) {

        List<WebUserSimpleEntity> webUsers = irenshiDubboProviderService.getWebUsers(companyId);
        if (CollectionUtils.isEmpty(webUsers)) {
            return Collections.emptyList();
        }
        List<SimpleAuthUserResourceVo> existedUsers = payrollService.getResourceUsers(companyId, bizModelAlias, resourceId);
        Map<String, SimpleAuthUserResourceVo> userIdMap = existedUsers.stream().collect(Collectors.toMap(SimpleAuthUserResourceVo::getUserId, s -> s));

        List<SimpleAuthUserResourceVo> result = Lists.newArrayListWithCapacity(webUsers.size());
        webUsers.forEach(user -> {
            SimpleAuthUserResourceVo userResourceVo;
            if (!userIdMap.containsKey(user.getId())) {
                userResourceVo = new SimpleAuthUserResourceVo();
                userResourceVo.setUserId(user.getId());
            } else {
                userResourceVo = userIdMap.get(user.getId());
            }
            userResourceVo.setUserAccount(user.getWebUserName());
            userResourceVo.setUserName(user.getMobileNo());

            result.add(userResourceVo);
        });
        return sortUsers(result);
    }

    private List<SimpleAuthUserResourceVo> sortUsers(List<SimpleAuthUserResourceVo> resourceUsers) {
        resourceUsers.sort(Comparator.comparing(SimpleAuthUserResourceVo::getUserName));

        List<SimpleAuthUserResourceVo> result = Lists.newArrayListWithCapacity(resourceUsers.size());
        resourceUsers.forEach(user -> {
            if (user.getPrivilege() == AccessPrivilege.OWNER) {
                result.add(0, user);
            } else {
                result.add(user);
            }
        });

        return result;
    }

}
